#include "kdcaudiovideoeditormodel.h"

#include <KLocale>
#include <kio/job.h>

#include <QPoint>
#include <QDebug>

#include <kdcnamespace.h>
#include <kdclib.h>

KDCAudioVideoEditorModel::KDCAudioVideoEditorModel( QObject *aParent ) : QAbstractTableModel( aParent ), pMovie( NULL )
{

}

KDCAudioVideoEditorModel::~KDCAudioVideoEditorModel()
{

}

int KDCAudioVideoEditorModel::rowCount( const QModelIndex &aParent ) const
{
	if ( pMovie )
		return 1 + pMovie->audio().size();
	return 0;
}

QVariant KDCAudioVideoEditorModel::data( const QModelIndex &aIndex, int aRole ) const
{
	if ( aRole == Qt::DisplayRole )
	{
		QString tmp( "" );
		switch ( aIndex.column() )
		{
			case KDCAudioVideoEditorModel::col_Type: return ( aIndex.row() == 0 ) ? i18n("Video") : i18n("Audio");
			case KDCAudioVideoEditorModel::col_Mode: return ( aIndex.row() == 0 ) ? toString( pMovie->video()->mode() ) : toString( pMovie->audio().at( aIndex.row() - 1 )->mode() );
			case KDCAudioVideoEditorModel::col_Codec: return ( aIndex.row() == 0 ) ? toString( pMovie->video()->codec() ) : toString( pMovie->audio().at( aIndex.row() - 1 )->codec() );
			case KDCAudioVideoEditorModel::col_Channels: return ( aIndex.row() == 0 ) ? tmp : QString::number( pMovie->audio().at( aIndex.row() - 1 )->channelCount() );
			case KDCAudioVideoEditorModel::col_System: return ( aIndex.row() == 0 ) ? toString( pMovie->videoFormat() ) : tmp;
			case KDCAudioVideoEditorModel::col_Bitrate: return ( aIndex.row() == 0 ) ? bitrateAsString( pMovie->video()->bitrate() ) : bitrateAsString( pMovie->audio().at( aIndex.row() - 1 )->bitrate() );
			case KDCAudioVideoEditorModel::col_Fps: return ( aIndex.row() == 0 ) ? QString::number( pMovie->fps() ) : tmp;
			case KDCAudioVideoEditorModel::col_Ratio: return ( aIndex.row() == 0 ) ? toString( pMovie->video()->aspectRatio() ) : tmp;
			case KDCAudioVideoEditorModel::col_Resolution: return ( aIndex.row() == 0 ) ? QString( "%1x%2" ).arg( pMovie->video()->width() ).arg( pMovie->video()->height() ) : tmp;
			case KDCAudioVideoEditorModel::col_Delay: return ( aIndex.row() == 0 ) ? tmp : QString::number( pMovie->audio().at( aIndex.row() - 1 )->delay() );
			case KDCAudioVideoEditorModel::col_Size: return ( aIndex.row() == 0 ) ? KIO::convertSizeFromKiB( pMovie->video()->size() ) : KIO::convertSizeFromKiB( pMovie->audio().at( aIndex.row() - 1 )->size() );
			default: return QVariant();
		}
	}
	if ( aRole == Qt::EditRole )
	{
		QVariant tmp;
		switch ( aIndex.column() )
		{
			case KDCAudioVideoEditorModel::col_Mode: return ( aIndex.row() == 0 ) ? QVariant( pMovie->video()->mode() ) : QVariant( pMovie->audio().at( aIndex.row() - 1 )->mode() );
			case KDCAudioVideoEditorModel::col_Codec: return ( aIndex.row() == 0 ) ? QVariant( pMovie->video()->codec() ) : QVariant( pMovie->audio().at( aIndex.row() - 1 )->codec() );
			case KDCAudioVideoEditorModel::col_Channels: return ( aIndex.row() == 0 ) ? tmp : QVariant( pMovie->audio().at( aIndex.row() - 1 )->channelCount() );
			case KDCAudioVideoEditorModel::col_System: return ( aIndex.row() == 0 ) ? QVariant( pMovie->videoFormat() ) : tmp;
			case KDCAudioVideoEditorModel::col_Fps: return ( aIndex.row() == 0 ) ?  QVariant( pMovie->fps() ) : tmp;
			case KDCAudioVideoEditorModel::col_Bitrate: return ( aIndex.row() == 0 ) ? QVariant( pMovie->video()->bitrate() ) : QVariant( pMovie->audio().at( aIndex.row() - 1 )->bitrate() );
			case KDCAudioVideoEditorModel::col_Ratio: return ( aIndex.row() == 0 ) ? QVariant( pMovie->video()->aspectRatio() ) : tmp;
			case KDCAudioVideoEditorModel::col_Resolution: return ( aIndex.row() == 0 ) ? QVariant( QPoint( pMovie->video()->width(), pMovie->video()->height() ) ) : tmp;
			case KDCAudioVideoEditorModel::col_Delay: return ( aIndex.row() == 0 ) ? tmp : QVariant( pMovie->audio().at( aIndex.row() - 1 )->delay() );
			case KDCAudioVideoEditorModel::col_Size: return ( aIndex.row() == 0 ) ? QVariant( pMovie->video()->size() ) : QVariant( pMovie->audio().at( aIndex.row() - 1 )->size() );
			default: return QVariant();
		}
	}
	if ( aRole == KDCAudioVideoEditorModel::role_DiscType )
		return getDisc( pMovie )->type();
	if ( aRole == Qt::CheckStateRole && aIndex.column() == KDCAudioVideoEditorModel::col_Check && aIndex.row() > 0 )
		return ( pMovie->audio().at( aIndex.row() - 1 )->isEnabled() ) ? Qt::Checked : Qt::Unchecked;
	return QVariant();
}

bool KDCAudioVideoEditorModel::setData( const QModelIndex &aIndex, const QVariant &aValue, int aRole )
{
	if ( aRole == Qt::EditRole )
	{
		//if ( aIndex.row() == tRow )
		//	return false;
		switch ( aIndex.column() )
		{
			case KDCAudioVideoEditorModel::col_Mode: ( aIndex.row() == 0 ) ? pMovie->video()->setMode( (KDC::StreamMode)aValue.toUInt() ) : pMovie->audio().at( aIndex.row() - 1 )->setMode( (KDC::StreamMode)aValue.toUInt() ); break;
			case KDCAudioVideoEditorModel::col_Codec: ( aIndex.row() == 0 ) ? pMovie->video()->setCodec( (KDC::VideoCodec)aValue.toUInt() ) : pMovie->audio().at( aIndex.row() - 1 )->setCodec( (KDC::AudioCodec)aValue.toUInt() ); break;
			case KDCAudioVideoEditorModel::col_Channels:
			{
				if ( aIndex.row() > 0 ) 
					pMovie->audio().at( aIndex.row() - 1 )->setChannelCount( aValue.toUInt() );
				break;
			}
			case KDCAudioVideoEditorModel::col_System:
			{
				if ( aIndex.row() == 0 )
					pMovie->setVideoFormat( (KDC::VideoFormat)aValue.toUInt() );
				break;
			}
			case KDCAudioVideoEditorModel::col_Fps:
			{
				if ( aIndex.row() == 0 )
					pMovie->setFps( aValue.toDouble() );
				break;
			}
			case KDCAudioVideoEditorModel::col_Bitrate: ( aIndex.row() == 0 ) ? pMovie->setFps( aValue.toUInt() ) : pMovie->audio().at( aIndex.row() - 1 )->setBitrate( aValue.toUInt() ); break;
			case KDCAudioVideoEditorModel::col_Ratio:
			{
				if ( aIndex.row() == 0 )
					pMovie->video()->setAspectRatio( (KDC::AspectRatio)aValue.toUInt() );
				break;
			}
			case KDCAudioVideoEditorModel::col_Resolution:
			{
				if ( aIndex.row() == 0 )
					pMovie->video()->setResolution( aValue.toPoint().x(),  aValue.toPoint().y()  );
				break;
			}
			case KDCAudioVideoEditorModel::col_Delay:
			{
				if ( aIndex.row() == 0 )
					pMovie->audio().at( aIndex.row() - 1 )->setDelay( aValue.toUInt() );
				break;
			}
		}
		emit dataChanged( aIndex, aIndex );
		return true;
	}
	else
		if ( aRole == Qt::CheckStateRole && aIndex.column() == KDCAudioVideoEditorModel::col_Check && aIndex.row() > 0 )
		{
			pMovie->audio().at( aIndex.row() - 1 )->setEnabled( ( aValue.toInt() == Qt::Checked ) ? true : false );
			emit dataChanged( aIndex, aIndex );
			return true;
		}
	return false;
}

QModelIndex KDCAudioVideoEditorModel::parent( const QModelIndex &aIndex ) const
{
	return QModelIndex();
}

int KDCAudioVideoEditorModel::columnCount( const QModelIndex &aParent ) const
{
	return 12;
}

Qt::ItemFlags KDCAudioVideoEditorModel::flags(const QModelIndex &aIndex ) const
{
	Qt::ItemFlags res = Qt::ItemIsEnabled | Qt::ItemIsSelectable;
	if ( aIndex.column() == KDCAudioVideoEditorModel::col_Check && aIndex.row() > 0 )
		res |= Qt::ItemIsUserCheckable;
	QList<int> list;
	if ( aIndex.row() == 0 )
		list << KDCAudioVideoEditorModel::col_Check << KDCAudioVideoEditorModel::col_Type << KDCAudioVideoEditorModel::col_Channels << KDCAudioVideoEditorModel::col_Delay << KDCAudioVideoEditorModel::col_Size;
	else
		list << KDCAudioVideoEditorModel::col_Check << KDCAudioVideoEditorModel::col_Type << KDCAudioVideoEditorModel::col_System << KDCAudioVideoEditorModel::col_Bitrate << KDCAudioVideoEditorModel::col_Ratio << KDCAudioVideoEditorModel::col_Resolution << KDCAudioVideoEditorModel::col_Size;
	if ( !list.contains( aIndex.column() ) )
		res |= Qt::ItemIsEditable;
    return res;
}

QVariant KDCAudioVideoEditorModel::headerData( int aSection, Qt::Orientation aOrientation, int aRole ) const
{
	if ( aRole == Qt::DisplayRole && aOrientation == Qt::Horizontal )
	{
		switch ( aSection )
		{
			case KDCAudioVideoEditorModel::col_Type: return i18n("Type");
			case KDCAudioVideoEditorModel::col_Mode: return i18n("Mode");
			case KDCAudioVideoEditorModel::col_Codec: return i18n("Codec");
			case KDCAudioVideoEditorModel::col_Channels: return i18n("Channels");
			case KDCAudioVideoEditorModel::col_System: return i18n("System");
			case KDCAudioVideoEditorModel::col_Fps: return i18n("Fps");
			case KDCAudioVideoEditorModel::col_Bitrate: return i18n("Bitrate");
			case KDCAudioVideoEditorModel::col_Ratio: return i18n("Ratio");
			case KDCAudioVideoEditorModel::col_Resolution: return i18n("Resolution");
			case KDCAudioVideoEditorModel::col_Delay: return i18n("Delay");
			case KDCAudioVideoEditorModel::col_Size: return i18n("Size");
			default: return QVariant();
		}
	}
	return QAbstractTableModel::headerData( aSection, aOrientation, aRole );
}

void KDCAudioVideoEditorModel::setMovie( KDCMovie *aMovie )
{
	qDebug() << aMovie;
	pMovie = aMovie;
	reset();
}
